package com.kehao.leetcode.basic.string;

import org.junit.Test;

public class StringSolution9 {

    public String longestCommonPrefix(String[] strs) {
        StringBuilder sb = new StringBuilder();
        int prefixLength = 0;
        int maxLength = 200;//最小的字符串的最大长度
        String minStr = "";
        for (String str:strs) {
            if(str.length()<maxLength){
                maxLength = str.length();
                minStr = str;
            }
        }
        for (int i = 0; i < minStr.length(); i++) {
            char c = minStr.charAt(i);
            for (String str:strs) {
                if(str.charAt(i)!=c){
                    return minStr.substring(0,prefixLength);
                }
            }
            prefixLength++;
        }
        return minStr;
    }

    @Test
    public void test01(){
        String[] strs =  {"flower","flow","flight"};
        String prefix = longestCommonPrefix(strs);
        System.out.println(prefix);
    }

}
